package app.util.image.action
{
	import app.util.*;
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.Point;
	
	public class FilmGrainAction extends AbstractAction
	{
		private var dark:Boolean;
		private var amount:Number;
		
		public function setValue(isDark:Boolean, n:Number):FilmGrainAction
		{
			this.dark = isDark;
			this.amount = n;
			return this;
		}
		
		public override function getDescription():String
		{
			return "Film Grain (" + (this.dark ? "Dark" : "Light") + 
						", " + this.amount.toFixed(0) + ")";
		}
		
		public override function getActionedBitmapData():BitmapData
		{
			var old:BitmapData = Mediator.getCurrentBitmapData();
			var ret:BitmapData = old.clone();
			var tmp:BitmapData = new BitmapData(ret.width, ret.height);
			if(this.dark)
			{
				tmp.noise(1, 255 - this.amount * 128, 255);
				tmp.applyFilter(tmp, tmp.rect, new Point(0, 0), new BlurFilter(1.5, 1.5, 1));	
				ret.draw(tmp, null, null, BlendMode.MULTIPLY);							
			}
			else
			{
				tmp.noise(1, 0, this.amount * 128);
				tmp.applyFilter(tmp, tmp.rect, new Point(0, 0), new BlurFilter(1.5, 1.5, 1));
				ret.draw(tmp, null, null, BlendMode.SCREEN);					
			}

			return ret;
		}
	}
}